package com.comdo.newup_loan.module.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.comdo.newup_loan.BankWebViewActivity;
import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.PenaltyBean;
import com.comdo.newup_loan.util.StringUtils;
import com.comdo.newup_loan.util.TitleMenuUtil;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/11/2.
 */

public class PenaltyActivity extends BaseActivity<PenaltyPersenter> implements PenaltyContact.View{
    @BindView(R.id.tv_amount_left)
    TextView mLeftText;
    @BindView(R.id.tv_vyj)
    TextView mVvjText;
    @BindView(R.id.ed_amount)
    EditText mAmountEdText;
    @BindView(R.id.btn_submit)
    Button submitBtn;
    private double penaltyAmount;
    //输入的金额
    private double payAmount;
    private double available;

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.layout_penalty;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if(Config.userInfo!=null){
            available = Double.parseDouble(Config.userInfo.getAvailable());
            penaltyAmount = Double.parseDouble(Config.userInfo.getPenaltyAmount());
            mLeftText.setText(StringUtils.getMoneyString(available));
            mVvjText.setText(StringUtils.getMoneyString(penaltyAmount));
        }

        new TitleMenuUtil(PenaltyActivity.this,"缴纳违约金",true);
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!StringUtils.isNull(mAmountEdText.getText().toString())){
                    payAmount=Double.parseDouble(mAmountEdText.getText().toString());
                    if(payAmount>penaltyAmount){
                        showToast("输入金额不能超过应缴金额！");
                        return;
                    }
                    if(payAmount>available){
                        showToast("账户余额不足！");
                        return;
                    }
                    if(payAmount<=0){
                        showToast("违约金最小输入金额：0.01元！");
                        return;
                    }
                    mPresenter.payPenaltyAmount(mAmountEdText.getText().toString());
                }
            }
        });
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void payPenaltyAmount(PenaltyBean penaltyBean) {
        if(penaltyBean!=null)
        mPresenter.getBankWeb(PenaltyActivity.this,penaltyBean.getRequestNo(),payAmount,penaltyBean.getTargetPlatformUserNo());
    }

    @Override
    public void getBankWeb(String url) {
        if(!StringUtils.isNull(url)){
            toXwWeb(url);
        }else{
            showToast("page not found");
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    public PenaltyPersenter getmPresenter() {
        return new PenaltyPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }
    private void toXwWeb(String url){
        finish();
        Intent intent=new Intent(PenaltyActivity.this, BankWebViewActivity.class);
        intent.putExtra("url",url);
        startActivity(intent);
    }
}
